package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import com.beans.AdminInfo;
import com.common.Common.EnumAdminState;
import com.jdbc.DBUtil;
import com.utils.PageInfo;

/**
 * 管理员登录
 * 
 * @author yqb
 */
public class AdminDao {
	/**
	 * 根据账号和密码登录
	 * 
	 * @param adminName 账号
	 * @param password  密码
	 * @return 管理员信息实体类,登录失败返回null
	 */
	public AdminInfo login(String adminName, String password) {
		String sql = "select a.*,b.roleName from adminInfo a,roleinfo b where adminName=? and password=? and a.roleId=b.id";
		return DBUtil.getSingleObject(sql, AdminInfo.class, adminName, password);
	}

	/**
	 * 根据id获取用户信息
	 * 
	 * @param id 用户id
	 * @return 管理员信息实体类,登录失败返回null
	 */
	public AdminInfo getAdminInfo(int id) {
		String sql = "select * from adminInfo where id=?";
		return DBUtil.getSingleObject(sql, AdminInfo.class, id);
	}

	/**
	 * 添加新用户
	 * 
	 * @param admin 用户信息
	 */
	public int addAdmin(AdminInfo admin) {
		String sql = "insert into admininfo(adminName,password,note,state) values ( ?,?,?,?)  ";
		return DBUtil.update(sql, admin.getAdminName(), admin.getPassword(), admin.getNote(), admin.getState());
	}
	
	/**
	 * 修改用户
	 * 
	 * @param admin 用户信息
	 */
	public int updateAdmin(AdminInfo admin) {
		String sql = " update admininfo set adminName=?,note=? where id= ?";
		return DBUtil.update(sql, admin.getAdminName(),admin.getNote(), admin.getId());
	}
	
	/**
	 * 修改用户角色
	 * 
	 * @param admin 用户信息
	 */
	public int updateRoleId(int id,int roleid) {
		String sql = " update admininfo set roleId=? where id= ?";
		return DBUtil.update(sql, roleid, id);
	}
	
	/**
	 * 修改用户密码
	 * 
	 * @param admin 用户信息
	 */
	public int updatePsw(int id,String oldpsw,String psw) {
		String sql = " update admininfo set password=? where id= ? and password=?";
		return DBUtil.update(sql, psw, id,oldpsw);
	}

	/**
	 * 查询是否重名
	 * 
	 * @param admin 用户名
	 */
	public boolean isJuna(String name) {
		long count = DBUtil.getScalar("select count(*) from adminInfo where adminName= ?", name);
		if (count > 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取用户列表
	 * 
	 * @param page 分页属性
	 */
	public List<AdminInfo> getAdminList(PageInfo page) {
		return DBUtil.getList(
				"(select a.* , b.roleName from adminInfo a , roleinfo b where a.roleId=b.id  and state !=?  union all select a.* , Null from adminInfo a  where roleId is Null  and state !=? ) order by id desc limit ?, ? ",
				AdminInfo.class, EnumAdminState.删除.getValue(),EnumAdminState.删除.getValue(), page.getBeginRow(), page.getPageSize());
	}

	/**
	 * 获取数据条目
	 */
	public int getAdminCount() {
		long count = DBUtil.getScalar("select count(*) from adminInfo where state !=?",EnumAdminState.删除.getValue());
		return Integer.parseInt(count + "");
	}

	/**
	 * 禁用用户
	 * 
	 * @param id 用户id
	 * @return
	 */
	public int stopAdmin(int id) {
		return DBUtil.update("update adminInfo set state=? where id= ?",EnumAdminState.锁定.getValue(), id);
	}

	/**
	 * 解锁用户
	 * 
	 * @param id 用户id
	 * @return
	 */
	public int startAdmin(int id) {
		return DBUtil.update("update adminInfo set state=? where id= ?", EnumAdminState.正常.getValue(),id);
	}

	/**
	 * 删除用户
	 * 
	 * @param id 用户id
	 * @return
	 */
	public int deleteAdmin(int id) {
		return DBUtil.update("update adminInfo set state=? where id= ?",EnumAdminState.删除.getValue(), id);
	}

	/**
	 * 批量删除用户
	 * 
	 * @param strs 用户id列表
	 * @return
	 */
	public void delAllAdmin(String[] strs) {
		PreparedStatement stm = null;
		try {
			Connection conn = DBUtil.getConn();
			String sql = "update adminInfo set state=? where id= ?";
			stm = conn.prepareStatement(sql);

			for (String str : strs) {
				stm.setInt(1,EnumAdminState.删除.getValue());
				stm.setString(2, str);
				stm.addBatch(); // 添到批处理中了
			}
			stm.executeBatch(); // 执行批处理

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, stm);
		}
	}

}
